home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / LIBRARY / CMPLTPAS / KSEARCH.PAS < prev    next >
Pascal/Delphi Source File  |  1988-07-24  |  3KB  |  61 lines

  1. {->>>>KeySearch<<<<--------------------------------------------}
  2. {                                                              }
  3. { Filename : KSEARCH.SRC -- Last Modified 7/14/88              }
  4. {                                                              }
  5. { This routine searches file Keys for key records containing   }
  6. { the key string contained in parameter MatchIt.  The method   }
  7. { is your classic binary search, and the key record type is    }
  8. { defined as the type show below:                              }
  9. {                                                              }
  10. {     KeyRec = RECORD                                          }
  11. {                Ref     : Integer;                            }
  12. {                KeyData : String30                            }
  13. {              END;                                            }
  14. {                                                              }
  15. { The function returns True if a matching record is found,     }
  16. { else False.                                                  }
  17. {                                                              }
  18. {     From: COMPLETE TURBO PASCAL 5.0  by Jeff Duntemann       }
  19. {    Scott, Foresman & Co., Inc. 1988   ISBN 0-673-38355-5     }
  20. {--------------------------------------------------------------}
  21.  
  22. FUNCTION KeySearch(VAR Keys     : KeyFile;
  23.                    VAR KeyRef   : Integer;
  24.                        MatchIt  : String80) : Boolean;
  25.  
  26. VAR High,Low,Mid : Integer;
  27.     SearchRec    : KeyRec;
  28.     Found        : Boolean;
  29.     Collided     : Boolean;
  30.     RecCount     : Integer;
  31.  
  32. BEGIN
  33.   KeyRef := 0;                 { Initialize variables      }
  34.   RecCount := FileSize(Keys);
  35.   High := RecCount;
  36.   Low := 0;
  37.   KeySearch := False; Found := False; Collided := False;
  38.   Mid := (Low + High) DIV 2;   { Calc first midpoint       }
  39.  
  40.   IF RecCount > 0 THEN         { Don't search if file empty}
  41.     REPEAT
  42.       Seek(Keys,Mid);          { Read midpoint record      }
  43.       Read(Keys,SearchRec);
  44.       { Collision between Mid & Low or Mid & High?   }
  45.       IF (Low = Mid) OR (High = Mid) THEN Collided := True;
  46.       IF MatchIt = SearchRec.KeyData THEN  { Found it! }
  47.         BEGIN
  48.           Found := True;           { Set found flag...    }
  49.           KeySearch := True;       { ...function value... }
  50.           KeyRef := SearchRec.Ref  { ...and file key      }
  51.         END
  52.       ELSE            { No luck...divide & try again  }
  53.         BEGIN
  54.           IF MatchIt > SearchRec.KeyData THEN Low := Mid
  55.             ELSE High := Mid; { Halve the field  }
  56.           Mid := (Low + High + 1) DIV 2;    { Recalc midpoint }
  57.           KeyRef := Mid { Save Mid in parm }
  58.         END
  59.     UNTIL Collided OR Found
  60. END;  
  61.